﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.DataAccess;
using SD.Commons.DataFilters;
namespace SD.Business
{
    /// <summary>
    /// Class contains business method for look up information
    /// </summary>
    public class LookupController
    {
        /// <summary>
        /// Method is used for looking up postcode and fulfil into textbox
        /// </summary>
        /// <param name="filter">filter</param>
        /// <param name="sort">sort type</param>
        /// <param name="startRowIndex">start row index</param>
        /// <param name="maximumRows">maximum rows</param>
        /// <returns></returns>
        public static IQueryable<Address> LookupPostcodes(AddressFilter filter, string sort, int startRowIndex, int maximumRows)
        {
            var addresses= Address.GetAddresses(filter).Skip(startRowIndex).Take(maximumRows);

            return addresses = SortAddressList(addresses, sort);
        }

        /// <summary>
        /// Business method to sort a address list
        /// </summary>
        /// <param name="addresses">address list</param>
        /// <param name="sort">sort type</param>
        /// <returns>sorted address list</returns>
        private static IQueryable<Address> SortAddressList( IQueryable<Address> addresses, string sort)
        {
            //begin sort address list
            switch (sort)
            {
                case "Address":
                    return addresses.OrderBy(a => a.AddressLine);

                case "Address DESC":
                    return addresses.OrderByDescending(a => a.AddressLine);

                case "PostCode":
                    return addresses.OrderBy(a => a.Postcode);

                case "PostCode DESC":
                    return addresses.OrderByDescending(a => a.Postcode);
                    
                case "Town":
                    return addresses.OrderBy(a => a.Town.TownName);

                case "Town DESC":
                    return addresses.OrderByDescending(a => a.Town.TownName);
                    
                case "County":
                    return addresses.OrderBy(a => a.Town.County.CountyName);

                case "County DESC":
                    return addresses.OrderByDescending(a => a.Town.County.CountyName);

                case "Country":
                    return addresses.OrderBy(a => a.Town.County.Country.CountryName);

                case "Country DESC":
                    return addresses.OrderByDescending(a => a.Town.County.Country.CountryName);
                
                default:
                    return addresses;
            }
            
        }

        /// <summary>
        /// Method is used for retrieve back number record of lookup address list
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static int GetLookupPostCodeCount(AddressFilter filter)
        {
            var count = Address.GetAddresses(filter).Count();
            return count;
        }

        public static Address GetAddressDetail(int id)
        {
            return Address.GetAddressById(id);
        }
        //----------------------VIDT-------------------------------------
      
        /// <summary>
        /// Prevents a default instance of the <see cref="LookupController"/> class from being created.
        /// </summary>
        private static readonly Contact Dao = new Contact();

        /// <summary>
        /// Finds the contact by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static Contact FindContactById(int id)
        {
            return Dao.FindContactById(id);
        }
        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <param name="startRowIndex">Start index of the row.</param>
        /// <param name="max">The max.</param>
        /// <returns>Contact list query</returns>
        public static IQueryable<Contact> GetListContact(ContactFilter filter, string sortType, int startRowIndex, int maxRows)
        {
            return Dao.GetListContact(filter, sortType, startRowIndex, maxRows);
        }
        /// <summary>
        /// Gets the list count.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns>Number of list count</returns>
        public static int GetListCountContact(ContactFilter filter)
        {
            return Dao.GetListCount(filter);
        }

        //-------------TrungL1----------------
        /// <summary>
        ///TypeOfBusinessDao object
        /// </summary>
        private static readonly TypeOfBusiness TypeOfBusinessDao = new TypeOfBusiness();

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <param name="startRowIndex">Start index of the row.</param>
        /// <param name="maxRows">The max rows.</param>
        /// <returns>BusinessType list</returns>
        public static IQueryable<BusinessType> GetList(BusinessFilters filter, string sortType, int startRowIndex,
                                                       int maxRows)
        {
            IQueryable<BusinessType> queryable = TypeOfBusinessDao.GetList(filter, sortType, startRowIndex, maxRows);
            return queryable;
        }

        /// <summary>
        /// Gets the list count.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public static int GetListCount(BusinessFilters filter)
        {
            int count = TypeOfBusinessDao.GetListCount(filter);
            return count;
        }

        /// <summary>
        /// Gets the business type by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static BusinessType GetBusinessTypeById(int id)
        {
            BusinessType results = TypeOfBusinessDao.GetBusinessTypeById(id);
            return results;
        }

        /// <summary>
        /// Gets the list query.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="filter">The filter.</param>
        /// <returns>Contact list</returns>
        public static IQueryable<Contact> GetListQuery(IQueryable<Contact> data, ContactFilter filter)
        {
            // Filter by first name
            if (filter.FirstName != null)
            {
                data = data.Where(c => c.FirstName.StartsWith(filter.FirstName));
            }
            // Filter by Surname name
            if (filter.Surname != null)
            {
                data = data.Where(c => c.Surname.StartsWith(filter.Surname));
            }

            // Filter by in-active & active contact
            if (!filter.IncludeInactive)
            {
                data = data.Where(c => c.IsActive);
            }

            return data;
        }
    }
}
